#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
using namespace std;

class Solution {
public:
    void duplicateZeros(vector<int>& arr) {
        int mov = 0;
        int count = 0;
        while (count < arr.size())
        {
            if (arr[mov] != 0)
            {
                ++count;
            }
            else
            {
                count += 2;
            }
            //cout<<count<<endl;
            ++mov;
        }
        mov -= 1;
        //cout<<mov<<endl;
        int write = arr.size() - 1;

        if (arr[mov] == 0 && count > arr.size())
            arr[write--] = arr[mov--];
        while (mov >= 0)
        {
            //cout<<"w:"<<write<<endl;
            //cout<<"m:"<<mov<<endl;
            if (arr[mov] != 0)
                arr[write--] = arr[mov];
            else
            {
                arr[write] = 0;
                if (write - 1 >= 0)
                    arr[write - 1] = 0;
                write -= 2;
            }
            --mov;
        }
    }
};